﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using MongoDB.Bson;
using Newtonsoft.Json.Linq;
using Org.BouncyCastle.Utilities.Encoders;
using System.Net;
using up6.db.model;
using up6.utils;
using Up6.core.Up6Manager;
using Up6.core.Up6Manager.Models.Req;


namespace Up6.core.Controllers;

[ApiController]
[Route("")]
public class DownController : ControllerBase
{
    private readonly DownAction act;
    private readonly IConfiguration _configuration;
    public DownController(DownAction svr, IConfiguration cfg)
    {
        act = svr;
        _configuration = cfg;
    }

    [HttpGet]
    [Route("api/down2/f_create")]
    public dynamic create(
        [FromQuery] string id,
        [FromQuery] string uid,
        [FromQuery] string fdTask,
        [FromQuery] string nameLoc,
        [FromQuery] string pathLoc,
        [FromQuery] long lenSvr,        
        [FromQuery] string sizeSvr,
        [FromQuery] string callback               
        )
    {
        var f = new DnFileInf();        
        f.id= id;
        f.uid= uid;
        f.nameLoc = WebUtility.UrlDecode(nameLoc);
        f.pathLoc = WebUtility.UrlDecode(pathLoc);
        f.lenSvr= lenSvr;
        f.sizeSvr = WebUtility.UrlDecode(sizeSvr);
        f.fdTask = fdTask == "1";        
        f.callback = callback;                       
        return act.Create(f);

    }    

    [HttpGet]
    [Route("api/down2/f_down")]

    public async Task<dynamic> down(
        [FromHeader] string id,                
        [FromHeader] int blockIndex,
        [FromHeader] long blockOffset,
        [FromHeader] int blockSize,
        [FromHeader] string pathSvr,
        [FromHeader] string object_key = "",        
        [FromHeader] string token = ""
        )
    {
        var f = new DnFileInf();
        f.id = id;
        f.blockIndex = blockIndex;
        f.blockOffset = blockOffset;        
        f.blockSize = blockSize;
        f.pathSvr = WebUtility.UrlDecode(pathSvr);
        f.object_key = object_key;        
        f.token = token;        

        return await act.downAsync(f, Response);

    }

    [HttpGet]
    [Route("api/down2/clear")]

    public dynamic clear()
    {
        up6.db.database.down2.sql.SqlFile.build().Clear();
        return new ContentResult
        {
            Content = "down_files表清空成功",
            ContentType = "application/json; charset=utf-8",
            StatusCode = 200
        };

    }

    [HttpGet]
    [Route("api/down2/f_del")]

    public dynamic del(
        [FromQuery] string id,
        [FromQuery] string uid,
        [FromQuery] string callback
        )
    {
        var f = new DnFileInf();
        f.id= id;
        f.uid= uid;
        f.callback= callback;
        return act.del(f);

    }

    [HttpGet]
    [Route("api/down2/f_list")]

    public dynamic list(
        [FromQuery] string uid,
        [FromQuery] string callback
        )
    {
        var f = new DnFileInf();
        f.uid= uid;
        f.callback = callback;
        return act.list(f);

    }

    [HttpGet]
    [Route("api/down2/f_list_cmp")]

    public dynamic list_cmp(
        [FromQuery] string uid,
        [FromQuery] string callback
        )
    {
        var f = new DnFileInf();
        f.uid = uid;
        f.callback = callback;
        return act.list_cmp(f);

    }

    [HttpGet]
    [Route("api/down2/f_update")]

    public dynamic update(
        [FromQuery] string id,
        [FromQuery] string uid,        
        [FromQuery] long lenLoc,
        [FromQuery] string perLoc,
        [FromQuery] string callback
        )
    {
        var f = new DnFileInf();
        f.id= id;
        f.uid = uid;        
        f.lenLoc= lenLoc;
        f.perLoc= perLoc;
        f.callback = callback;
        return act.update(f);

    }    

    [HttpGet]
    [Route("api/down2/fd_data")]

    public dynamic fd_data(
        [FromQuery] string id,        
        [FromQuery] string callback
        )
    {
        var f = new DnFileInf();
        f.id = id;        
        f.callback = callback;
        return act.fd_data(f);
    }
}
